package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.*;
import com.lczc.soa.mapper.PolicyMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PolicyServiceImpl implements PolicyService{

    @Autowired
    private PolicyMapper policyMapper;

    @Override
    public int savePolicy(Policy policy) {
        if(StringUtils.isNotBlank(policy.getPolicyId())){
            return policyMapper.updateByPrimaryKey(policy);
        }else {
            return policyMapper.insert(policy);
        }
    }

    @Override
    public SearchPolicyResponse findAllPolicy(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
//        List<Policy> policies = policyMapper.selectByExample(new PolicyExample());
        List<Policy> policies = policyMapper.selectAll();
//        for (Policy policy : policies){
//            System.out.println(policy.getPolicyId());
//        }
        return new SearchPolicyResponse(((Page)policies).getTotal(),policies);
    }

    @Override
    public SearchPolicyResponse findPolicyLikeName(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Policy> policies = policyMapper.selectByLikeName(keywords);
        return new SearchPolicyResponse(((Page)policies).getTotal(),policies);
    }

    @Override
    public Policy findPolicyByPrimaryKey(String id) {
        return policyMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deletePolicy(String id) {
        return policyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Policy> listPolicy() {
        return policyMapper.selectByExample(new PolicyExample());
    }

}
